package com.example.hzq.food.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hzq.food.R;
import com.example.hzq.food.adapter.Fragment4adapter;
import com.example.hzq.food.bean.MyBean;
import com.example.hzq.food.model.Model;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.food.activity.Login.userid;

/**
 * Created by hzq on 2017/6/14.
 */

public class Fragment4 extends Fragment implements View.OnClickListener{
    private RecyclerView recyclerView;
    private LinearLayout layouttop,layout1,layout2,layout3;/*layout5,6,7,8*/
    private TextView tv_name,tv_id,tv_phone;
    private TextView tv_ye,tv_precent,tv_integer;
    private  RecyclerView.LayoutManager layoutManager;
    private Fragment4adapter fragment4adapter;
    private List<MyBean> list=null;
    private Context context;
    private View view=null;
     public static int user_id;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view=inflater.inflate(R.layout.my,container,false);
        context=getActivity();
        init();
        getdatal();
        return view;
    }
    private void getdatal(){
        user_id=Integer.valueOf(userid);
        Model model=new Model();
      Call<MyBean>call=model.getmy(user_id);
        call.enqueue(new Callback<MyBean>() {
            @Override
            public void onResponse(Call<MyBean> call, Response<MyBean> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    tv_name.setText(response.body().getUsername());
                    tv_id.setText(response.body().getUser_id()+"");
                    tv_phone.setText(response.body().getMobilenum());
                }
                else{
                    Toast.makeText(context,"资源获取失败",Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<MyBean> call, Throwable t) {
                Toast.makeText(context, "404", Toast.LENGTH_SHORT).show();
            }
        });
        }
    private void init() {
        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview3);
        tv_name=(TextView)view.findViewById(R.id.textView22);
        tv_id=(TextView)view.findViewById(R.id.textView23);
        tv_phone=(TextView)view.findViewById(R.id.textView24);
        tv_ye=(TextView)view.findViewById(R.id.textview27);
        tv_precent=(TextView)view.findViewById(R.id.textview28);
        tv_integer=(TextView)view.findViewById(R.id.textview29);
        layouttop=(LinearLayout)view.findViewById(R.id.linearlayout5);
        layout1=(LinearLayout)view.findViewById(R.id.linearlayout6);
        layout2=(LinearLayout)view.findViewById(R.id.linearlayout7);
        layout3=(LinearLayout)view.findViewById(R.id.linearlayout8);
        layouttop.setOnClickListener(this);
        layout1.setOnClickListener(this);
        layout2.setOnClickListener(this);
        layout3.setOnClickListener(this);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        list=getData();
        fragment4adapter=new Fragment4adapter(context);
       fragment4adapter.setList(list);
        recyclerView.setAdapter(fragment4adapter);
        tv_ye.setText("999.00"+"元");
        tv_precent.setText("10"+"个");
        tv_integer.setText("99999"+"分");
    }
    private List<MyBean> getData(){
        List<MyBean> list=new ArrayList<MyBean>();
        MyBean art;
        art=new MyBean();
        art.setItemname("收货地址");
        art.setImgid(R.drawable.ic_location);
        art.setItemntro("");
        list.add(art);

        art=new MyBean();
        art.setItemname("我的收藏");
        art.setImgid(R.drawable.ic_collect1);
        art.setItemntro("");
        list.add(art);

        art=new MyBean();
        art.setItemname("推荐有奖");
        art.setImgid(R.drawable.ic_recommend);
        art.setItemntro("5元现金");
        list.add(art);

        art=new MyBean();
        art.setItemname("积分商城");
        art.setImgid(R.drawable.ic_integral);
        art.setItemntro("0元好物在这里");
        list.add(art);

        art=new MyBean();
        art.setItemname("每日精选");
        art.setImgid(R.drawable.ic_select);
        art.setItemntro("1元起秒杀");
        list.add(art);

        art=new MyBean();
        art.setItemname("免费流量");
        art.setImgid(R.drawable.ic_wifi);
        art.setItemntro("没有最高500M");
        list.add(art);

        art=new MyBean();
        art.setItemname("服务中心");
        art.setImgid(R.drawable.ic_servicecenter);
        art.setItemntro("");
        list.add(art);

        art=new MyBean();
        art.setItemname("欢迎评分");
        art.setImgid(R.drawable.ic_score);
        art.setItemntro("");
        list.add(art);

        art=new MyBean();
        art.setItemname("加盟合作");
        art.setImgid(R.drawable.ic_join);
        art.setItemntro("");
        list.add(art);
        return list;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.linearlayout5:
                Toast.makeText(context,"应显示用户详情",Toast.LENGTH_SHORT).show();
                break;
           default:
               break;
        }
    }

}
